from flask import Flask, request

import const
from utils import request_util, redis_util, response_util, file_util
import timer_task
from service import face_service, mongo_service
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address

'''
POST param in requset.form
GET param in request.args
'''
# init
timer_task.startTask()
timer_task.start_consuming()
app = Flask(__name__)
limiter = Limiter(
    app,
    key_func=get_remote_address,
    default_limits=["10 per second"]
)

@limiter.exempt
@app.route('/test', methods=['POST'])
def test():
    a = request_util.get_param(request, 'a')
    b = request_util.get_param(request, 'b')
    c = request_util.get_param(request, 'c')
    url = request_util.get_param(request, 'url')
    print(a)
    print(b)
    print(c)
    print(url)
    print(request_util.get_header(request, 'X-Auth-Token'))
    return response_util.success(msg='ssss')

#@limiter.exempt
@app.route('/verify_face', methods=['POST'])
def verify_face():
    res = check_token(request)
    if not res['status']:
        return response_util.error('need login')
    username = request_util.get_username_by_request(request)
    file_list = request.files.getlist('file')
    return face_service.verify_face(username, file_list)

@app.route('/input_face', methods=['POST'])
def input_face():
    res = check_token(request)
    if not res['status']:
        return response_util.error('need login')
    stu_info = redis_util.convert_to_dict(res['stu_info'])
    file_list = request.files.getlist('file')
    return face_service.upload_face_imgs(stu_info, file_list)

@app.route('/verify_android', methods=['POST'])
def verify_android_result():
    res = check_token(request)
    stu_info = redis_util.convert_to_dict(res['stu_info'])
    if not res['status']:
        return response_util.error('need login')
    stu_no = stu_info['studentNo']
    attendence_id = request_util.get_param('attendence_id')
    md5 = request_util.get_param('md5')
    threshold = request_util.get_param('threshold')
    return face_service.verify_face_res_for_android(stu_no, attendence_id, md5, threshold)

@app.route('/get_embedding_txt_context', methods=['GET'])
def get_embedding_txt_context():
    # student_no
    '''
    res = check_token(request)
    if not res['status']:
        return response_util.error('need login')
    stu_no = request_util.get_username_by_request(request)
    '''
    stu_no = 'XS000003'
    #return response_util.error('sssssssssssss')
    #return face_service.get_embedding_for_android(stu_no)
    face_service.get_embedding_for_android(stu_no)
    return 'sssssssssss'



def check_token(request):
    token = request_util.get_header(request, 'X-Auth-Token')
    username = request_util.get_username_by_token(token)
    token_key = file_util.generate_token_key(username, token)
    redis_conn = redis_util.get_connect()
    stu_info = redis_conn.get(token_key)
    data = {
        'stu_info': stu_info,
        'status': True
    }
    if stu_info == None:  # no login
        data['status'] = False
    return data

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000)